import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const user = {
  namespaced: true,
  state: {
    userInfo: null,
    token: null
  },
  mutations: {
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    clearUserInfo(state) {
      state.userInfo = null
    },
    setToken(state, token) {
      state.token = token
    },
    clearToken(state) {
      state.token = null
    }
  },
  getters: {
    isLoggedIn: (state) => !!state.token || !!state.userInfo,
    userId: (state) => (state.userInfo && state.userInfo.id) ? state.userInfo.id : null
  }
}

const store = new Vuex.Store({
  modules: {
    user
  }
})

export default store